/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;

import java.awt.Color;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import otros.Fecha;
import otros.MiTableModel;
import otros.Util;
import tablasSql.Clientes;
import tablasSql.ConsultaBD;
import tablasSql.FacturasAlbaranes;
import tablasSql.Sql;

/**
 *
 * @author FERRETERIA
 */

public class FFacturas extends javax.swing.JInternalFrame {    
    boolean fuera;
    int filaSel;
    //int columSel;
    private final static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(FFacturas.class);
     
    public FFacturas() {
        Util.setConfiguracionLog(Util.LOG4_FICH);
        initComponents();
        tabla.setAutoCreateRowSorter(true);
        tabla.setColumnSelectionAllowed(false);
        
        ActionMap mapaAccion = this.getActionMap();
        InputMap map = this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        //negro
        KeyStroke ctrl_n = KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK);
        map.put(ctrl_n , "ctrl_n");
        mapaAccion.put("ctrl_n",CTRL_N());
        
        String []fecha = new Fecha().getFecha().split("-");
        txtDia.setText(fecha[0]);
        txtMes.setText(fecha[1]);
        txtAño.setText(fecha[2]);
        chNegro.setVisible(false);
        ActualizarTabla();
        Colorear();
    }

    public AbstractAction CTRL_N(){
        return new AbstractAction() { 
                    public void actionPerformed(ActionEvent e) {
                        if (cbTipo.getSelectedIndex() < 2)
                            chNegro.setVisible(!chNegro.isVisible());} 
                };
    }
    
    private void Colorear() {
        Color color = Util.getColorTicket(cbTipo.getSelectedItem().toString());
        cbTipo.setBackground(color);
        cbVendedor.setBackground(color);
        cbTipoPago.setBackground(color);
        txtIdAño.setBackground(color);
        txtDia.setBackground(color);
        txtMes.setBackground(color);
        txtAño.setBackground(color);
        txtIdCliente.setBackground(color);
        txtId.setBackground(color);
    }
    
    private void AnchoColumnas() {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)tabla.getColumnModel();
        TableColumn col = colModel.getColumn(0);        
        col.setPreferredWidth(100);
        col = colModel.getColumn(1);
        col.setPreferredWidth(320);
        int valor = 25;
        
        switch (cbTipo.getSelectedIndex()) {
            case 1:
                if (chNegro.isSelected()) {
                    tabla.getColumnModel().getColumn(6).setMaxWidth(valor);
                    tabla.getColumnModel().getColumn(6).setMinWidth(valor);
                    tabla.getColumnModel().getColumn(6).setPreferredWidth(valor);    
                    break;
                }
            case 2:
                tabla.getColumnModel().getColumn(5).setMaxWidth(valor);
                tabla.getColumnModel().getColumn(5).setMinWidth(valor);
                tabla.getColumnModel().getColumn(5).setPreferredWidth(valor);    
                break;
            case 3:
                tabla.getColumnModel().getColumn(5).setMaxWidth(100);
                tabla.getColumnModel().getColumn(6).setMaxWidth(valor);
                tabla.getColumnModel().getColumn(6).setMinWidth(valor);
                tabla.getColumnModel().getColumn(6).setPreferredWidth(valor);    
                break;
        }
        
        bBorrar.setEnabled(false);
        bConvertir.setEnabled(false);
    }
    
    private void ActualizarTabla() {  
        cboxSelTodo.setVisible((cbTipo.getSelectedIndex()==1)?true:false);
        cboxSelTodo.setSelected(false);
        int idAno = Integer.parseInt(txtIdAño.getText().isEmpty()? "0" :txtIdAño.getText());
        int id = Integer.parseInt(txtId.getText().isEmpty()? "0" :txtId.getText());
        int tipo = cbTipoPago.getSelectedIndex();
        int idCliente = Integer.parseInt(txtIdCliente.getText().isEmpty()? "-1" : txtIdCliente.getText());
        int dia = Integer.parseInt(txtDia.getText().isEmpty()? "0" : txtDia.getText());
        int mes = Integer.parseInt(txtMes.getText().isEmpty()? "0" : txtMes.getText());
        int año = Integer.parseInt(txtAño.getText().isEmpty()? "0" : txtAño.getText());
        boolean negro = (cbTipo.getSelectedIndex() < 2)? chNegro.isSelected(): false;
        int tam = tabla.getRowCount();
        for (int i = 0; i < tam; i++)
            ((DefaultTableModel)tabla.getModel()).removeRow(0); 
        MiTableModel datos = FacturasAlbaranes.getTabla(negro, cbVendedor.getSelectedIndex(), cbTipo.getSelectedIndex(),
                                                    idAno, id, tipo, idCliente, dia, mes, año);
        int colSelect = 5;
        switch(cbTipo.getSelectedIndex()) {
            case 1:
                if(chNegro.isSelected()) {
                    datos.addColumn("", new Vector<Boolean>());
                    datos.setType(new Class[] {String.class, String.class,String.class, 
                        Float.class, String.class,Float.class, Boolean.class});
                    datos.setEditable(new Boolean[] {false,false,false,false,false,false,true});
                    break;
                }
            case 2:
                datos.addColumn("", new Vector<Boolean>());
                datos.setType(new Class[] {String.class, String.class,String.class, Float.class, String.class, Boolean.class});
                datos.setEditable(new Boolean[] {false,false,false,false,false,true});
                break;
            case 3:
                colSelect = 6;
                datos.addColumn("", new Vector<Boolean>());
                datos.setType(new Class[] {String.class, String.class,String.class, Float.class, 
                                            String.class, String.class, Boolean.class});
                datos.setEditable(new Boolean[] {false,false,false,false,false,false,true});
                break;    
        } 
       
        tabla.setModel(datos);
        tabla.getColumnModel().getColumn(0).setCellRenderer(new tableCellRender.Id(filaSel, fuera));
        tabla.getColumnModel().getColumn(1).setCellRenderer(new tableCellRender.Texto(filaSel, fuera));
        tabla.getColumnModel().getColumn(2).setCellRenderer(new tableCellRender.TipoTicket(filaSel, fuera));
        tabla.getColumnModel().getColumn(3).setCellRenderer(new tableCellRender.Precio(filaSel, fuera));
        tabla.getColumnModel().getColumn(4).setCellRenderer(new tableCellRender.Fecha(filaSel, fuera));
        if (cbTipo.getSelectedIndex()==3)
            tabla.getColumnModel().getColumn(5).setCellRenderer(new tableCellRender.Fecha(filaSel, fuera));
        
        txtCreditoNegro.setVisible(false);
        lblCreditoNegro.setVisible(false);
        txtSubTotal.setVisible(false);
        txtTotal.setVisible(false);
        jLabel7.setVisible(false);
        
        if(negro) {
            tabla.getColumnModel().getColumn(5).setCellRenderer(new tableCellRender.Precio(filaSel, fuera));
            txtSubTotal.setVisible(true);
            txtTotal.setVisible(true);
            jLabel7.setVisible(true);
            
            if (!txtIdCliente.getText().isEmpty() && (cbTipoPago.getSelectedIndex()==2)) {  //credito
                txtCreditoNegro.setVisible(true);
                lblCreditoNegro.setVisible(true);
                lblCreditoNegro.setText("Credito Cliente");
                txtCreditoNegro.setText(String.valueOf(Clientes.getCredito(txtIdCliente.getText())).replace(".", ",")+"€");
            } 
            else if (chNegro.isVisible() && (cbTipoPago.getSelectedIndex()==1) && (cbTipo.getSelectedIndex()==0)
                    && chNegro.isSelected()) {          //negro
                txtCreditoNegro.setVisible(true);
                lblCreditoNegro.setVisible(true);
                lblCreditoNegro.setText("Total Negro");
                float suma = 0;
                for (int i = 0; i < tabla.getRowCount(); i++)
                    suma += Float.parseFloat(""+tabla.getValueAt(i, 5));
                txtCreditoNegro.setText(String.valueOf(suma).replace(".",",")+"€");
            }
        }
        
        txtNumReg.setText(""+tabla.getRowCount());
        AnchoColumnas();
        HallarTotal();
    }
    
    private void Borrar(String valor) {
       int id = FacturasAlbaranes.getIdFacturaAlbaran(valor.substring(5), valor.substring(0,4),cbTipo.getSelectedIndex());
       Util.restaurarStock(id, cbTipo.getSelectedIndex(), true);
       FacturasAlbaranes.borrarFactAlbaran(id, cbTipo.getSelectedIndex());
       FacturasAlbaranes.borrarListArticulos(id, cbTipo.getSelectedIndex());
       
       log.info("borrarFactAlbaran " + cbTipo.getSelectedItem().toString() + ": " + valor);
    }
    
    private void Convertir(int aConvertir) {
        boolean primero = true;
        int newIdT = -1;
        String newId = "";
        ConsultaBD.cursorEspera(this);
        int pos = 0;
        if (tabla.isEditing())
            tabla.editingStopped(null);
        int colum = ((cbTipo.getSelectedIndex() == 3) || 
                ((cbTipo.getSelectedIndex() == 1) && chNegro.isSelected())) ? 6 : 5;
        for (int i = 0; i < tabla.getRowCount(); i++) {
            if ((tabla.getValueAt(i,colum) != null) && ((Boolean) tabla.getValueAt(i, colum)).booleanValue()) {
                String valor = "" + tabla.getValueAt(i, 0);
                int id = FacturasAlbaranes.getIdFacturaAlbaran(valor.substring(5), valor.substring(0,4),cbTipo.getSelectedIndex());
                if (primero) {      // primer albaran a pasar
                    primero = false;
                    ResultSet res = FacturasAlbaranes.getFacturaAlbaran(id, cbTipo.getSelectedIndex());
                    try {
                        if (!res.next()) {
                            return ;  
                        }
                        //--------- datos que se guardaran
                        ArrayList<String> v = new ArrayList<String> ();
                        String fecha = new Fecha().getFechaSQL()+ " " + new Fecha().getHora();
                        v.add(fecha);
                        v.add(fecha);       //fechacrea
                        v.add(res.getString("observacion"));
                        v.add("" + res.getFloat("total")); 
                        v.add("" + res.getInt("descuento")); 
                        v.add("" + res.getFloat("totalmano"));
                        v.add(null);        //negro
                        v.add(null);        //entrego
                        v.add(null);        //pagoEn
                        v.add("" + res.getInt("idCliente"));
                        if (aConvertir == 0)            // factura
                            v.add("0");                 // contado  
                        else
                            v.add("1");                 // credito
                        v.add(null);        // vendedor
                        String sqlTabla = FacturasAlbaranes.getTablaSql(aConvertir);
                        
                        String idAño = JOptionPane.showInputDialog(this,
                                                            "Año de la factura (201X)",
                                                            new Fecha().getAño());
                        int año = Integer.parseInt(idAño);
                        int idImp = Sql.getMax("idImp",sqlTabla + " where idAno = " + año)+1;
                        v.add("" + idImp);
                        v.add("" + año);
                        // paso el albaran a factura
                        newIdT = FacturasAlbaranes.guardar(v.toArray(new String[v.size()]), true, aConvertir);  
                        DecimalFormat df = new DecimalFormat("000000");
                        newId = año +"-"+ df.format(idImp);
                    } catch (SQLException ex) {
                        log.error("Convertir: " + ex);
                    } catch (NumberFormatException ex) {
                        log.error("Convertir: " + ex);
                    }
                }
                if (newIdT != -1) {
                    // borro albaran, presupuesto, fiado
                    FacturasAlbaranes.borrarFactAlbaran(id, cbTipo.getSelectedIndex());
                    //lo logueo para ver cuando se hizo
                    String seConvierte = aConvertir == 0 ? "Factura":"Albaran";
                    log.info("Convertir " + cbTipo.getSelectedItem().toString() + ":" + valor
                                    + " a " + seConvierte + ":" + newId);    
                    //inserto el numero de albaran en la lista
                    if(cbTipo.getSelectedIndex()==1) {
                        DecimalFormat df = new DecimalFormat("000000");
                        String [] fecha =  tabla.getValueAt(i, 4).toString().substring(0,10).split("-");
                        String datos3 [] = {cbTipo.getSelectedItem().toString() + " nº: " +
                                            valor.substring(0,4)+"-"+df.format(Integer.parseInt(valor.substring(5))) +
                                             " Fecha: " + fecha[2] + "/" + fecha[1] + "/" + fecha[0], //detalle
                                            null, //margen
                                            null, //descuento
                                            null, //precio
                                            null, //total
                                            null, //cantidad
                                            null,
                                            "0",
                                            ""+pos, //pos
                                            ""+newIdT,
                                       };
                        FacturasAlbaranes.guardarLista(datos3);
                    }
                
                    // modifico los articulos
                    String datos2[] = {""+newIdT, ""+aConvertir,""+(pos++), "" + id, ""+cbTipo.getSelectedIndex()}; 
                    pos += FacturasAlbaranes.convertir(datos2);
                }
            }  
        }
        ConsultaBD.cursorNormal(this);
        if (newIdT != -1)
            abrirFactura(newId, aConvertir);
        
    }
    
    
    private void abrirFactura(String id, int tipo) {
        ConsultaBD.cursorEspera(this);
        FTPV tpv = new FTPV();
        this.getParent().add(tpv);
        Principal.centrarFrame(tpv);
        tpv.show();    
        tpv.cargar(id, tipo);
        ConsultaBD.cursorNormal(this);
    }
    
    private void HallarTotal() {
        float total = 0;
        for(int i = 0; i < tabla.getRowCount(); i++) {
            total += Float.parseFloat(tabla.getValueAt(i, 3).toString());
        }
        txtTotal.setText(Util.getFormatPrecio(total,2));
    }
    
    private float HallarSubtotal(int colum) {
        float subTotal = 0;
        for(int i = 0; i < tabla.getRowCount(); i++) {
            if (Boolean.parseBoolean(""+tabla.getValueAt(i, colum))) {
                subTotal += Float.parseFloat(tabla.getValueAt(i,3).toString().replace("€","").replace(",", ".").trim());
            }
        }
        return subTotal;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel4 = new javax.swing.JLabel();
        txtNumReg = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbTipo = new javax.swing.JComboBox();
        txtId = new javax.swing.JFormattedTextField();
        txtIdCliente = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        cbTipoPago = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtDia = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        txtMes = new javax.swing.JFormattedTextField();
        bBuscar = new javax.swing.JButton();
        txtIdAño = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        chNegro = new javax.swing.JCheckBox();
        cbVendedor = new javax.swing.JComboBox();
        txtAño = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        bConvertir = new javax.swing.JButton();
        bBorrar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        cboxSelTodo = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        txtCreditoNegro = new javax.swing.JTextField();
        lblCreditoNegro = new javax.swing.JLabel();
        txtSubTotal = new javax.swing.JTextField();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestion Ventas");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel4.setText("Nº reg");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 22, 35);
        getContentPane().add(jLabel4, gridBagConstraints);

        txtNumReg.setEditable(false);
        txtNumReg.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNumReg.setMinimumSize(new java.awt.Dimension(80, 20));
        txtNumReg.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(txtNumReg, gridBagConstraints);

        jScrollPane1.setMinimumSize(new java.awt.Dimension(400, 250));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(400, 250));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Cliente", "Total", "Fecha", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setToolTipText("Boton dch raton abre la factura");
        tabla.setColumnSelectionAllowed(true);
        tabla.getTableHeader().setReorderingAllowed(false);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tablaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tablaMouseExited(evt);
            }
        });
        tabla.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                tablaMouseMoved(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);
        tabla.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        if (tabla.getColumnModel().getColumnCount() > 0) {
            tabla.getColumnModel().getColumn(0).setResizable(false);
            tabla.getColumnModel().getColumn(1).setResizable(false);
            tabla.getColumnModel().getColumn(2).setResizable(false);
            tabla.getColumnModel().getColumn(3).setResizable(false);
            tabla.getColumnModel().getColumn(4).setResizable(false);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jPanel1.setMinimumSize(new java.awt.Dimension(700, 70));
        jPanel1.setPreferredSize(new java.awt.Dimension(700, 70));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Criterios de busqueda:   ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        jPanel1.add(jLabel1, gridBagConstraints);

        cbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Factura", "Albaran", "Presupuesto", "Fiado" }));
        cbTipo.setMinimumSize(new java.awt.Dimension(100, 20));
        cbTipo.setPreferredSize(new java.awt.Dimension(100, 20));
        cbTipo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipoItemStateChanged(evt);
            }
        });
        cbTipo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cbTipoKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(cbTipo, gridBagConstraints);

        txtId.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("000000"))));
        txtId.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtId.setMinimumSize(new java.awt.Dimension(130, 20));
        txtId.setPreferredSize(new java.awt.Dimension(60, 20));
        txtId.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtIdMouseClicked(evt);
            }
        });
        txtId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtIdKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        jPanel1.add(txtId, gridBagConstraints);

        txtIdCliente.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtIdCliente.setMinimumSize(new java.awt.Dimension(100, 20));
        txtIdCliente.setPreferredSize(new java.awt.Dimension(40, 20));
        txtIdCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtIdClienteMouseClicked(evt);
            }
        });
        txtIdCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtIdClienteKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel1.add(txtIdCliente, gridBagConstraints);

        jLabel6.setText("Id Cliente");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        jPanel1.add(jLabel6, gridBagConstraints);

        cbTipoPago.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Contado", "Credito", "Vale" }));
        cbTipoPago.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipoPagoItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(cbTipoPago, gridBagConstraints);

        jLabel2.setText("Dia");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 8);
        jPanel1.add(jLabel2, gridBagConstraints);

        txtDia.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtDia.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDia.setMinimumSize(new java.awt.Dimension(25, 20));
        txtDia.setPreferredSize(new java.awt.Dimension(25, 20));
        txtDia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDiaMouseClicked(evt);
            }
        });
        txtDia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDiaKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        jPanel1.add(txtDia, gridBagConstraints);

        jLabel3.setText("Mes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel1.add(jLabel3, gridBagConstraints);

        txtMes.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtMes.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMes.setMinimumSize(new java.awt.Dimension(25, 20));
        txtMes.setPreferredSize(new java.awt.Dimension(25, 20));
        txtMes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMesMouseClicked(evt);
            }
        });
        txtMes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMesKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 25, 0, 0);
        jPanel1.add(txtMes, gridBagConstraints);

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        bBuscar.setMinimumSize(new java.awt.Dimension(61, 61));
        bBuscar.setPreferredSize(new java.awt.Dimension(61, 61));
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(bBuscar, gridBagConstraints);

        txtIdAño.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("0000"))));
        txtIdAño.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtIdAño.setToolTipText("yyyy");
        txtIdAño.setMinimumSize(new java.awt.Dimension(25, 20));
        txtIdAño.setPreferredSize(new java.awt.Dimension(35, 20));
        txtIdAño.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtIdAñoMouseClicked(evt);
            }
        });
        txtIdAño.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtIdAñoKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtIdAño, gridBagConstraints);

        jLabel5.setText("-");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jLabel5, gridBagConstraints);

        chNegro.setText("Negro");
        chNegro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chNegroActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 2;
        jPanel1.add(chNegro, gridBagConstraints);

        cbVendedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Ale", "Onan", "Tino", "Juan" }));
        cbVendedor.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbVendedorItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
        jPanel1.add(cbVendedor, gridBagConstraints);

        txtAño.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("0000"))));
        txtAño.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAño.setToolTipText("yyyy");
        txtAño.setMinimumSize(new java.awt.Dimension(25, 20));
        txtAño.setPreferredSize(new java.awt.Dimension(35, 20));
        txtAño.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtAñoMouseClicked(evt);
            }
        });
        txtAño.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAñoKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 30, 0, 0);
        jPanel1.add(txtAño, gridBagConstraints);

        jLabel8.setText("Año");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        jPanel1.add(jLabel8, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 30);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setPreferredSize(new java.awt.Dimension(160, 60));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        bConvertir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/factura.png"))); // NOI18N
        bConvertir.setToolTipText("<html><p align=center>El albaran o albaranes se convierte <br> a factura eliminando los albaranes</p>");
        bConvertir.setMinimumSize(new java.awt.Dimension(60, 60));
        bConvertir.setPreferredSize(new java.awt.Dimension(60, 60));
        bConvertir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bConvertirActionPerformed(evt);
            }
        });
        jPanel2.add(bConvertir, new java.awt.GridBagConstraints());

        bBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/eliminar.png"))); // NOI18N
        bBorrar.setToolTipText("Eliminar la linea seleccionada");
        bBorrar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBorrar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBorrarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel2.add(bBorrar, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        getContentPane().add(jPanel2, gridBagConstraints);

        jPanel3.setPreferredSize(new java.awt.Dimension(160, 60));
        jPanel3.setLayout(new java.awt.GridBagLayout());

        cboxSelTodo.setText("Seleccionar todo");
        cboxSelTodo.setToolTipText("");
        cboxSelTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxSelTodoActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        jPanel3.add(cboxSelTodo, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 10);
        getContentPane().add(jPanel3, gridBagConstraints);

        jPanel4.setMinimumSize(new java.awt.Dimension(200, 60));
        jPanel4.setPreferredSize(new java.awt.Dimension(250, 60));
        jPanel4.setLayout(new java.awt.GridBagLayout());

        jLabel7.setText("Total");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        jPanel4.add(jLabel7, gridBagConstraints);

        txtTotal.setEditable(false);
        txtTotal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTotal.setMinimumSize(new java.awt.Dimension(80, 20));
        txtTotal.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel4.add(txtTotal, gridBagConstraints);

        txtCreditoNegro.setEditable(false);
        txtCreditoNegro.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCreditoNegro.setMinimumSize(new java.awt.Dimension(70, 20));
        txtCreditoNegro.setPreferredSize(new java.awt.Dimension(70, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        jPanel4.add(txtCreditoNegro, gridBagConstraints);

        lblCreditoNegro.setText("Credito Cliente");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        jPanel4.add(lblCreditoNegro, gridBagConstraints);

        txtSubTotal.setEditable(false);
        txtSubTotal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtSubTotal.setMinimumSize(new java.awt.Dimension(80, 20));
        txtSubTotal.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 5);
        jPanel4.add(txtSubTotal, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        getContentPane().add(jPanel4, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 34) {
            int i = cbTipo.getSelectedIndex();
            if (i > cbTipo.getItemCount()) 
               i = 0;
            else
                i++;
            cbTipo.setSelectedIndex(i);
        }
    }//GEN-LAST:event_formKeyPressed

    private void cbTipoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbTipoKeyPressed
        if (evt.getKeyCode() == 10) 
            cbTipo.transferFocus();
    }//GEN-LAST:event_cbTipoKeyPressed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        int fila = tabla.rowAtPoint(evt.getPoint());
        int columna = tabla.columnAtPoint(evt.getPoint());
        
        if (evt.isMetaDown()) {
           abrirFactura(""+tabla.getValueAt(fila, 0), cbTipo.getSelectedIndex());
        }
        else {
            if (cbTipo.getSelectedIndex() < 2) {    // facturas y albaranes
                // mismo dia
                if (String.valueOf(tabla.getValueAt(fila,4)).substring(0,10).equals(new Fecha().getFechaSQL()))
                    bBorrar.setEnabled(true);
                else
                    // no es contado
                    if (Integer.parseInt(""+tabla.getValueAt(fila,2))>0)
                        bBorrar.setEnabled(true);
                    else {
                        if (columna < 5) {
                            int op = JOptionPane.showConfirmDialog(null, 
                                                        "¿Esta seguro? (la caja de ese dia se vera modificada)",
                                                        "Borrar Factura", JOptionPane.OK_CANCEL_OPTION, 
                                                        JOptionPane.INFORMATION_MESSAGE);
                            bBorrar.setEnabled(true);
                            if (op == 2)
                                bBorrar.setEnabled(false);
                        } else
                            bBorrar.setEnabled(false);
                    }
            }
            else                                    // presupuesto y vales
                 bBorrar.setEnabled(true);
        }    
        
        if (cbTipo.getSelectedIndex() > 0) {
            int colum = ((cbTipo.getSelectedIndex() == 3) ||
                         ((cbTipo.getSelectedIndex() == 1) && chNegro.isSelected()))? 6: 5;
            if (columna == colum) {                     //seleccion
               Boolean valor = false;
               float subTotal = 0;
               if (cbTipo.getSelectedIndex() == 1) { //albaran
                    for (int i = 0; (i < tabla.getRowCount()) && !valor; i++) {
                        if (Boolean.parseBoolean(""+tabla.getValueAt(i, colum))) {
                           valor = Boolean.parseBoolean(""+tabla.getValueAt(i, colum));
                           //bBorrar.setEnabled(false);
                        }
                    }
                    subTotal = HallarSubtotal(colum);
               }
               else {
                   // solo puedo elegir uno
                   Boolean aux = Boolean.parseBoolean(""+tabla.getValueAt(fila, colum));
                   for (int i = 0; (i < tabla.getRowCount()); i++) {
                       if (i != fila)
                          tabla.setValueAt(new Boolean(false), i, colum);
                       else 
                           subTotal = aux ? Float.parseFloat(tabla.getValueAt(i,3).toString().replace("€","").replace(",", ".").trim())
                                   : 0;
                   }
                   valor = aux;
               }
               bConvertir.setEnabled(valor);
               txtSubTotal.setText(Util.getFormatPrecio(subTotal, 2));
            }
        }
    }//GEN-LAST:event_tablaMouseClicked

    private void txtIdClienteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdClienteKeyPressed
        switch (evt.getKeyCode()) {
            case 113:
                FClientes c = new FClientes(this, Util.FFACTURAS);
                this.getParent().add(c);
                Principal.centrarFrame(c);
                c.show();
                break;
            case 10:
                ActualizarTabla();
                break;
        }
    }//GEN-LAST:event_txtIdClienteKeyPressed

    private void tablaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseExited
        fuera = true;
    }//GEN-LAST:event_tablaMouseExited

    private void tablaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseEntered
        fuera = false;
    }//GEN-LAST:event_tablaMouseEntered

    private void tablaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseMoved
        JTable aTable =  (JTable)evt.getSource();
        if (!fuera) {
            filaSel = aTable.rowAtPoint(evt.getPoint());
        }
    }//GEN-LAST:event_tablaMouseMoved

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        ActualizarTabla();
    }//GEN-LAST:event_bBuscarActionPerformed

    private void txtIdClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtIdClienteMouseClicked
        txtIdCliente.selectAll();
    }//GEN-LAST:event_txtIdClienteMouseClicked

    private void txtIdMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtIdMouseClicked
        txtId.selectAll();
    }//GEN-LAST:event_txtIdMouseClicked

    private void txtDiaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDiaMouseClicked
        txtDia.selectAll();
    }//GEN-LAST:event_txtDiaMouseClicked

    private void txtMesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMesMouseClicked
        txtMes.selectAll();
    }//GEN-LAST:event_txtMesMouseClicked

    private void bConvertirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bConvertirActionPerformed
        switch (cbTipo.getSelectedIndex()){
            case 1:
                if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, 
                                                "¿Esta seguro? (los albaranes seran borrados)",
                                                "Transformar a Factura", JOptionPane.OK_CANCEL_OPTION)) {
                    Convertir(0);
                }
                break;
            case 2:
            case 3:
                int seleccion = JOptionPane.showOptionDialog(this, "Seleccione opcion", "En que se convertira", 
                                    JOptionPane.YES_NO_CANCEL_OPTION,                    
                                    JOptionPane.QUESTION_MESSAGE,
                                    null,    // null para icono por defecto.
                                    new Object[] { "Factura", "Albaran"},   // null para YES, NO y CANCEL
                                    "Factura");
                if (seleccion != -1) 
                    Convertir(seleccion);
                break;
        }
        ActualizarTabla();
    }//GEN-LAST:event_bConvertirActionPerformed

    private void bBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBorrarActionPerformed
        int fila = tabla.getSelectedRow();
        if (fila > -1) {
            String mensaje = "<html><p align=center> ¿Esta seguro de borrar?<br> PD: Asegurece de que no esta abierta la factura </p>";
            if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, mensaje,"Borrar", JOptionPane.OK_CANCEL_OPTION)) {
               Borrar(""+tabla.getValueAt(fila, 0));
               ActualizarTabla();
            }
        }
    }//GEN-LAST:event_bBorrarActionPerformed

    private void cbTipoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipoItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            if (cbTipo.getSelectedIndex() > 1) {
               chNegro.setVisible(false);
               chNegro.setSelected(false);   
            }
            switch (cbTipo.getSelectedIndex()) {
                case 3:     // fiado
                    txtDia.setText("");
                    txtMes.setText("");
                    txtAño.setText("");
                    break;
            }
            ActualizarTabla();
            Colorear();
        }
    }//GEN-LAST:event_cbTipoItemStateChanged

    private void txtIdAñoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtIdAñoMouseClicked
        txtIdAño.selectAll();
    }//GEN-LAST:event_txtIdAñoMouseClicked

    private void txtIdAñoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdAñoKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
            txtId.requestFocus();
        }
    }//GEN-LAST:event_txtIdAñoKeyReleased

    private void txtIdKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtIdKeyReleased

    private void txtDiaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDiaKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
            txtMes.requestFocus();
        }
    }//GEN-LAST:event_txtDiaKeyReleased

    private void txtMesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMesKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtMesKeyReleased

    private void cbTipoPagoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipoPagoItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_cbTipoPagoItemStateChanged

    private void cboxSelTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxSelTodoActionPerformed
        if (tabla.isEditing()) 
            tabla.editingStopped(null);
        int colum = (chNegro.isSelected() && (cbTipo.getSelectedIndex()==1)) ? 6: 5;
        for (int i = 0; i < tabla.getRowCount(); i++) 
            tabla.setValueAt(cboxSelTodo.isSelected(), i, colum);
        if (cbTipo.getSelectedIndex() == 1) { //albaran
            bConvertir.setEnabled(cboxSelTodo.isSelected());
            txtSubTotal.setText(Util.getFormatPrecio(HallarSubtotal(colum), 2));
        }
        
    }//GEN-LAST:event_cboxSelTodoActionPerformed

    private void chNegroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chNegroActionPerformed
        ActualizarTabla();
    }//GEN-LAST:event_chNegroActionPerformed

    private void cbVendedorItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbVendedorItemStateChanged
        ActualizarTabla();
    }//GEN-LAST:event_cbVendedorItemStateChanged

    private void txtAñoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAñoMouseClicked
        txtAño.selectAll();
    }//GEN-LAST:event_txtAñoMouseClicked

    private void txtAñoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAñoKeyReleased
        ActualizarTabla();
    }//GEN-LAST:event_txtAñoKeyReleased

         
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBorrar;
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bConvertir;
    private javax.swing.JComboBox cbTipo;
    private javax.swing.JComboBox cbTipoPago;
    private javax.swing.JComboBox cbVendedor;
    private javax.swing.JCheckBox cboxSelTodo;
    private javax.swing.JCheckBox chNegro;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCreditoNegro;
    private javax.swing.JTable tabla;
    private javax.swing.JFormattedTextField txtAño;
    private javax.swing.JTextField txtCreditoNegro;
    private javax.swing.JFormattedTextField txtDia;
    private javax.swing.JFormattedTextField txtId;
    private javax.swing.JFormattedTextField txtIdAño;
    public javax.swing.JTextField txtIdCliente;
    private javax.swing.JFormattedTextField txtMes;
    private javax.swing.JTextField txtNumReg;
    private javax.swing.JTextField txtSubTotal;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables
}
